package lion.dev.web;

import java.io.BufferedReader;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * @author lion
 * @mail hl_0074@sina.com
 * @date 2011-9-13
 */
public class WebUtil {

	// forward attr
	private static final String FORWARD_REQUEST_URI_ATTRIBUTE = "javax.servlet.forward.request_uri";
	private static final String FORWARD_CONTEXT_PATH_ATTRIBUTE = "javax.servlet.forward.context_path";
	private static final String FORWARD_SERVLET_PATH_ATTRIBUTE = "javax.servlet.forward.servlet_path";
	private static final String FORWARD_PATH_INFO_ATTRIBUTE = "javax.servlet.forward.path_info";
	private static final String FORWARD_QUERY_STRING_ATTRIBUTE = "javax.servlet.forward.query_string";

	// include attr
	private static final String INCLUDE_REQUEST_URI_ATTRIBUTE = "javax.servlet.include.request_uri";
	private static final String INCLUDE_CONTEXT_PATH_ATTRIBUTE = "javax.servlet.include.context_path";
	private static final String INCLUDE_SERVLET_PATH_ATTRIBUTE = "javax.servlet.include.servlet_path";
	private static final String INCLUDE_PATH_INFO_ATTRIBUTE = "javax.servlet.include.path_info";
	private static final String INCLUDE_QUERY_STRING_ATTRIBUTE = "javax.servlet.include.query_string";

	// error attr
	private static final String ERROR_MESSAGE_ATTRIBUTE = "javax.servlet.error.message";
	private static final String ERROR_EXCEPTION_ATTRIBUTE = "javax.servlet.error.exception";

	public static void exposeForwardAttribute(HttpServletRequest request) {

		setAttribute(request, FORWARD_REQUEST_URI_ATTRIBUTE, request.getRequestURI());
		setAttribute(request, FORWARD_CONTEXT_PATH_ATTRIBUTE, request.getContextPath());
		setAttribute(request, FORWARD_SERVLET_PATH_ATTRIBUTE, request.getServletPath());
		setAttribute(request, FORWARD_PATH_INFO_ATTRIBUTE, request.getPathInfo());
		setAttribute(request, FORWARD_QUERY_STRING_ATTRIBUTE, request.getQueryString());
	}

	public static void exposeIncludeAttribute(HttpServletRequest request) {

		setAttribute(request, INCLUDE_REQUEST_URI_ATTRIBUTE, request.getRequestURI());
		setAttribute(request, INCLUDE_CONTEXT_PATH_ATTRIBUTE, request.getContextPath());
		setAttribute(request, INCLUDE_SERVLET_PATH_ATTRIBUTE, request.getServletPath());
		setAttribute(request, INCLUDE_PATH_INFO_ATTRIBUTE, request.getPathInfo());
		setAttribute(request, INCLUDE_QUERY_STRING_ATTRIBUTE, request.getQueryString());
	}

	public static void exposeErrorAttribute(HttpServletRequest request, Throwable ex) {

		setAttribute(request, ERROR_EXCEPTION_ATTRIBUTE, ex);
		setAttribute(request, ERROR_MESSAGE_ATTRIBUTE, ex.getMessage());
	}

	private static void setAttribute(HttpServletRequest request, String attr, Object value) {

		request.setAttribute(attr, value);
	}

	public static String getRemoteIP(HttpServletRequest request) {

		String ip = request.getHeader("X-Forwarded-For");

		if (ip != null) {
			if (ip.indexOf(',') == -1) { return ip; }
			return ip.split(",")[0];
		}

		if (StringUtils.isBlank(ip) || StringUtils.equals(ip, "unknown")) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (StringUtils.isBlank(ip) || StringUtils.equals(ip, "unknown")) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (StringUtils.isBlank(ip) || StringUtils.equals(ip, "unknown")) {
			ip = request.getRemoteAddr();
		}

		return ip;
	}

	public static String getRequestBody(ServletRequest req) {

		HttpServletRequest request = (HttpServletRequest) req;

		StringBuilder sbu = new StringBuilder();
		String tmp = null;
		BufferedReader br = null;
		try {
			br = request.getReader();
			while ((tmp = br.readLine()) != null) {
				sbu.append(tmp).append("\n");
			}
		} catch (Exception e) {
		}

		return sbu.toString();
	}
}
